var ws, aid, uid, spage, status;
var shares=null,bhref=false;
var Intent=null,File=null,Uri=null,main=null;
if(window.plus){
	plusReady();
}else{
	document.addEventListener("plusready",plusReady,false);
}
// H5 plus事件处理
function plusReady(){
	updateSerivces();
	if(plus.os.name=="Android"){
		Intent = plus.android.importClass("android.content.Intent");
		File = plus.android.importClass("java.io.File");
		Uri = plus.android.importClass("android.net.Uri");
		main = plus.android.runtimeMainActivity();
	}
	ws = plus.webview.currentWebview();
	aid = ws.aid;
	spage = ws.spage;
	uid = plus.storage.getItem('uid');//用户id
	status = plus.storage.getItem('status');
//	console.log(userid +" "+sellerId);
	$('.qq').on('tap',function(){
		if(!uid){
			toast('你还未登录请先登录');return;
		}
		shareChange(0);
	})
//	$('#kone').on('tap',function(){shareChange(0);})
	$('.wxsline').on('tap',function(){
		if(!uid){
			toast('你还未登录请先登录');return;
		}
		shareChange(1);
	})
	$('.weixin').on('tap',function(){
		if(!uid){
			toast('你还未登录请先登录');return;
		}
		shareChange(2);
	})
}
/**
 * 更新分享服务
 */
function updateSerivces(){
	plus.share.getServices( function(s){
		shares={};
		for(var i in s){
			var t=s[i];
			shares[t.id]=t;
		}
	}, function(e){
		plus.nativeUI.toast( "获取分享服务列表失败："+e.message );
	} );
}
/**
 * 弹出分享平台选择
 */
function shareChange(i){
	var ids=[{id:"qq"},{id:"weixin",ex:"WXSceneTimeline"},{id:"weixin",ex:"WXSceneSession"},{id:"sinaweibo"}];
 	var s = shares[ids[i].id];
// 	console.log(JSON.stringify(s))
	if ( s.authenticated ) {
		shareMessage(shares[ids[i].id],ids[i].ex);
	} else {
		s.authorize( function(){
				shareMessage(shares[ids[i].id],ids[i].ex);
			},function(e){
			plus.nativeUI.alert( "认证授权失败"+e.message,null,'提示' );
		});
	}
}
	/**
   * 发送分享消息
   * @param {plus.share.ShareService} s
   */
function shareMessage(s,ex){
	console.log(aid);
	var msg={title : '绥夏医生' , extra : {scene:ex}  , content:"亲，您的好友向你推荐绥夏医生app中的一则互动圈消息！",href:apiRoot+'?m=home&c=index&a=share&aid='+aid,thumbs:['/images/iconic.png']};
	s.send( msg, function(){
		$.ajax({
			type:"get",
			url:apiRoot+"?m=home&c=bbs&a=addForward",
			data : {
				aid :aid,
				uid :uid
			},
			success :function(data){
				console.log(JSON.stringify(data));
				if(data > 0){
					toast( "转发成功");
					plus.webview.currentWebview().reload();
					if(spage){
						plus.webview.getWebviewById(spage).reload();
					}
				}
			},
			error :function(e){
				console.log(JSON.stringify(e));
			}
		});
	}, function(e){
//		console.log(JSON.stringify(e));
		plus.nativeUI.toast( "转发失败" );
	});
}
